import service
import graphicWrappers
import blue
import collections
import geo2
import trinity
import uthread

class LightClientComponent():
    __guid__ = 'component.LightClientComponent'


class LightClient(service.Service):
    __guid__ = 'svc.lightClient'
    __componentTypes__ = ['light']
    __dependencies__ = ['graphicClient']
    LIGHT_TYPES = {const.world.BASIC_LIGHT: trinity.Tr2InteriorLightSource,
     const.world.BOX_LIGHT: trinity.Tr2InteriorBoxLight}

    def __init__(self):
        service.Service.__init__(self)
        self.isServiceReady = False



    def Run(self, *etc):
        service.Service.Run(self, *etc)
        self.isServiceReady = True



    def CreateComponent(self, name, state):
        component = LightClientComponent()
        lightClass = self.LIGHT_TYPES.get(state.GetLightType())
        renderObject = lightClass()
        component.renderObject = renderObject
        graphicWrappers.Wrap(renderObject, inInterior=True)
        renderObject.SetColor(state.GetColor())
        if (state.GetLightType() == const.world.BASIC_LIGHT):
            renderObject.SetRadius(state.GetRadius())
            renderObject.coneDirection = state.GetConeDirection()
            renderObject.coneAlphaInner = state.GetConeAlphaInner()
            renderObject.coneAlphaOuter = state.GetConeAlphaOuter()
        else:
            renderObject.SetScaling(state.GetScaling())
        renderObject.SetFalloff(state.GetFalloff())
        renderObject.shadowImportance = state.GetShadowImportance()
        renderObject.primaryLighting = state.GetPrimaryLighting()
        renderObject.secondaryLighting = state.GetSecondaryLighting()
        renderObject.secondaryLightingMultiplier = state.GetSecondaryLightingMultiplier()
        renderObject.affectTransparentObjects = state.GetAffectTransparentObjects()
        renderObject.shadowResolution = state.GetShadowResolution()
        renderObject.shadowCasterTypes = state.GetShadowCasterTypes()
        renderObject.projectedTexturePath = state.GetProjectedTexturePath().encode()
        renderObject.isStatic = state.GetIsStatic()
        return component



    def ReportState(self, component):
        report = collections.OrderedDict()
        report['Has RenderObject'] = (component.renderObject is not None)
        if component.renderObject:
            report['Position'] = component.renderObject.GetPosition()
        return report



    def PrepareComponent(self, sceneID, entityID, component):
        component.renderObject.name = str(entityID)
        worldspace = sm.GetService('worldSpaceClient').GetWorldSpaceInstance(sceneID)
        worldspace.LoadPlaceableToScene(component.renderObject)
        scene = self.graphicClient.GetScene(sceneID)
        component.renderObject.AddToScene(scene)
        uthread.worker('lightClient::_RefreshLight', self._RefreshLight, component.renderObject)



    def _RefreshLight(self, renderObject):
        blue.synchro.Sleep(1000)
        t = renderObject.coneAlphaOuter
        renderObject.coneAlphaOuter = 0
        renderObject.coneAlphaOuter = t



    def SetupComponent(self, entity, component):
        positionComponent = entity.GetComponent('position')
        if positionComponent:
            component.renderObject.SetPosition(positionComponent.position)
            if hasattr(component.renderObject, 'SetRotationYawPitchRoll'):
                component.renderObject.SetRotationYawPitchRoll(geo2.QuaternionRotationGetYawPitchRoll(positionComponent.rotation))



    def UnRegisterComponent(self, entity, component):
        scene = self.graphicClient.GetScene(entity.scene.sceneID)
        component.renderObject.RemoveFromScene(scene)




